package com.zoke.daycarpool.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.os.Bundle;

import com.zoke.daycarpool.R;
import com.zoke.daycarpool.adapter.MyPublishAdapter;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.config.CarpoolParamStore;
import com.zoke.daycarpool.config.UrlBank;
import com.zoke.daycarpool.domain.MinePubBeen;
import com.zoke.daycarpool.domain.Token;
import com.zoke.daycarpool.domain.UserBeen;
import com.zoke.daycarpool.domain.UserInstense;
import com.zoke.daycarpool.parser.MyPublishParser;
import com.zoke.daycarpool.util.LogTest;
import com.zoke.support.http.HttpStores;
import com.zoke.support.http.ZokeErro;
import com.zoke.support.http.ZokeParams;
import com.zoke.support.view.XListView;

/**
 * 我的发布
 * 
 * @author jack
 * 
 */
public class MyPublish extends BaseUi {
	private XListView mListView;
	private List<MinePubBeen> mList = new ArrayList<MinePubBeen>();
	private MyPublishAdapter mAdapter;

	private UserBeen mUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mypublish);
		mUser = UserInstense.getInstense(getApplicationContext()).getUser();
		mTitleManager.setTitle("我的发布");
		mTitleManager.setLeftIcon(R.drawable.title_back);
		mEnableSwipeRight = true;
		mAdapter = new MyPublishAdapter(MyPublish.this, mList);
		findView();
		doEvent();
		fetchMyPublish();
	}

	@Override
	public void onSuccess(ZokeParams out) {
		super.onSuccess(out);
		String result = (String) out.getResult();
		LogTest.tag("result=" + result);
		MyPublishParser parser = new MyPublishParser();

		try {
			List<MinePubBeen> list = parser.parseJson(result);
			Token.getTokener().updateToken(parser.getToken());
			if (parser.getCode() == 0) {
				if (list != null && list.size() != 0) {
					mList.clear();
					mList.addAll(list);
					mAdapter.notifyDataSetChanged();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onFails(ZokeParams out) {
		super.onFails(out);
		ZokeErro erro = (ZokeErro) out.getResult();
		LogTest.tag("erro=" + erro.getErrorNo());
	}

	/** 拉取我的发布 **/
	private void fetchMyPublish() {
		ZokeParams param = CarpoolParamStore.getDefaultParam(mHashCode);
		param.setUrl(UrlBank.doMyPublish);
		param.put("sid", "" + mUser.getUserId());
		param.setUseCache(true);
		// param.put("timestamp", "");
		HttpStores.getInstense(getApplicationContext()).get(param);
	}

	@Override
	protected void findView() {
		mListView = (XListView) findViewById(R.id._listView);
		mListView.setAdapter(mAdapter);
	}

	@Override
	protected void doEvent() {
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);
	}

}
